package com.smday.sparsearray;

import java.io.*;

/**
 * @author Summerday
 */
public class SparseArray {

    public static void main(String[] args){

        //创建原始的二维数组 11*11

        //0表示没有棋子,1表示黑子,2表示蓝子
        int[][] chessArray1 = new int[11][11];
        chessArray1[1][2] = 1;
        chessArray1[2][3] = 2;

        System.out.println("====原始的二维数组====");
        for (int[] row : chessArray1) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

        //将二维数组转为稀疏数组
        //先遍历数组,得到非0的个数
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArray1[i][j] != 0) {
                    sum++;
                }
            }
        }

        //创建对应的稀疏数组
        int[][] sparseArr = new int[sum + 1][3];

        //给稀疏数组赋值
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;

        //遍历二维数组,将非0的值存入稀疏数组
        //用于记录第几个非零数据
        int count = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArray1[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArray1[i][j];
                }
            }
        }

        //输出稀疏数组
        System.out.println("==== 稀疏数组 ====");
        for (int[] row : sparseArr) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

        //将稀疏数组恢复为原始的二维数组

        //先读取第一行,根据第一行的数据,创建原始的二维数组

        int[][] chessArr2 = new int[sparseArr[0][0]][sparseArr[0][1]];

        //读取稀疏数组后几行的数据,从第二行开始,赋给原始的二维数组
        for (int i = 1; i < sparseArr.length; i++) {
            chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }



        //输出恢复后的二维数组
        System.out.println("恢复后的二维数组");

        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

        System.out.println("写入文件");
        try(BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\DS-AL\\DataStructures\\src\\map.data"))) {
            for (int[] ints : sparseArr) {
                for (int anInt : ints) {
                    System.out.println(anInt);
                    writer.write(""+anInt);
                }
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("写入成功");

        System.out.println("读取文件");
        try(BufferedReader reader = new BufferedReader(new FileReader("D:\\DS-AL\\DataStructures\\src\\map.data"))){
            String str;
            while((str = reader.readLine())!=null){
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
